import unittest


class TestReadFileMethods(unittest.TestCase):

    def test_read_file(self):
        f = open("a.txt", "r", encoding="utf-8")
        # 一次性读取全部内容
        print(f.read())
        f.close()

    def test_readline(self):
        f = open("a.txt", "r", encoding="utf-8")
        line = f.readline()
        # readline读取不到内容时返回为'', 而不是python的None.
        while line != '':
            print(line)
            line = f.readline()
        print(line == '')
        f.close()

    """
    说明:
        1. 使用with关键字打开文件并读写是不需要手动关闭的, 会自动关闭打开的文件.
    """
    def test_use_with_read(self):
        with open("a.txt", "r", encoding="utf-8") as f:
            print("文件名", f.name)
            line = f.readline()
            # readline读取不到内容时返回为'', 而不是python的None.
            while line != '':
                print(line)
                line = f.readline()
    

if __name__ == '__main__':
    unittest.main()
